package com.viphuli.http;

import android.app.Activity;

import com.loopj.android.http.RequestParams;
import com.offroader.R;
import com.offroader.http.HttpHelper;
import com.offroader.http.MyJsonHttpResponseHandler;
import com.offroader.utils.LogUtils;
import com.offroader.utils.StringUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.common.AppConfig;
import com.viphuli.common.MyPageHelper;
import com.viphuli.http.handler.AccountLoginResponseHandler;
import com.viphuli.http.handler.AccountLoginVCodeResponseHandler;
import com.viphuli.http.handler.AccountLoginVCodeVoiceResponseHandler;
import com.viphuli.http.handler.AccountNurseJoinReadyResponseHandler;
import com.viphuli.http.handler.AccountResponseHandler;
import com.viphuli.http.handler.AccountShareResponseHandler;
import com.viphuli.http.handler.AccountTicketResponseHandler;
import com.viphuli.http.handler.CommonMsgResponseHandler;
import com.viphuli.http.handler.HomeCompanyOrderHandler;
import com.viphuli.http.handler.HomePrivateCustomHandler;
import com.viphuli.http.handler.HomePrivateCustomInfoHandler;
import com.viphuli.http.handler.HomeResponseHandler;
import com.viphuli.http.handler.HomeServiceHandler;
import com.viphuli.http.handler.HomeServiceInfoCommentHandler;
import com.viphuli.http.handler.HomeServiceInfoHandler;
import com.viphuli.http.handler.MyBaseHttpResponseHandler;
import com.viphuli.http.handler.MyBaseHttpResponseHandler.HandlerType;
import com.viphuli.http.handler.PurchaseAutoFillAlreadyOrderResponseHandler;
import com.viphuli.http.handler.PurchaseAutoFillPhoneResponseHandler;
import com.viphuli.http.handler.PurchaseOrderCancelResponseHandler;
import com.viphuli.http.handler.PurchaseOrderFillInfoResponseHandler;
import com.viphuli.http.handler.PurchaseOrderFillResponseHandler;
import com.viphuli.http.handler.PurchaseOrderInfoResponseHandler;
import com.viphuli.http.handler.PurchaseOrderPayAlipayResponseHandler;
import com.viphuli.http.handler.PurchaseOrderPayInfoResponseHandler;
import com.viphuli.http.handler.PurchaseOrderPayOfflineResponseHandler;
import com.viphuli.http.handler.PurchaseOrderPayPriceResponseHandler;
import com.viphuli.http.handler.PurchaseOrderPayResultHandler;
import com.viphuli.http.handler.PurchaseOrderPayWeichatResponseHandler;
import com.viphuli.http.handler.PurchaseOrderResponseHandler;

/**
 * APP接口请求
 * 
 * @author li.li
 *
 */
public enum ApiRequest {

	home("首页", "/home/index", ApiTypeEnum.get, HandlerType.loading, HomeResponseHandler.class),

	homePrivateCustom("私人定制", "/home/room_serivce/custom/private", ApiTypeEnum.post, HandlerType.waitting, HomePrivateCustomHandler.class),

	homePrivateCustomInfo("私人定制信息自动填充", "/account/user/info", ApiTypeEnum.get, HandlerType.waitting, HomePrivateCustomInfoHandler.class),

	@Deprecated
	homeCompanyOrder("企业预约", "/home/room_serivce/order/company", ApiTypeEnum.post, HomeCompanyOrderHandler.class),

	homeRoomService("上门体检&上门护理", "/home/room_serivce/list", ApiTypeEnum.get, HandlerType.loading, HomeServiceHandler.class),

	homeRoomServiceInfo("服务详情", "/home/room_serivce/item_info", ApiTypeEnum.get, HandlerType.loading, HomeServiceInfoHandler.class),

	homeRoomServiceComment("服务详情评论", "/home/room_serivce/order/comment/list", ApiTypeEnum.get, HandlerType.loading,
			HomeServiceInfoCommentHandler.class),

	purchaseOrder("订单列表", "/purchase/order/list", ApiTypeEnum.get, HandlerType.loading, PurchaseOrderResponseHandler.class),

	purchaseOrderCancel("取消订单", "/purchase/order/cancel", ApiTypeEnum.get, HandlerType.waitting, PurchaseOrderCancelResponseHandler.class),

	purchaseOrderInfo("订单详情", "/purchase/order/info", ApiTypeEnum.get, HandlerType.loading, PurchaseOrderInfoResponseHandler.class),

	purchaseOrderPayInfo("订单支付信息", "/purchase/order/pay/info", ApiTypeEnum.get, HandlerType.loading, PurchaseOrderPayInfoResponseHandler.class),

	purchaseInstructions("服务介绍", "/purchase/instructions", ApiTypeEnum.h5),

	purchaseOrderFill("提交订单", "/purchase/order/create", ApiTypeEnum.post, HandlerType.waitting, PurchaseOrderFillResponseHandler.class),

	purchaseAutoFillReOrder("填充表单（再次预订）", "/purchase/order/info", ApiTypeEnum.get, HandlerType.loading, PurchaseOrderFillInfoResponseHandler.class),

	purchaseAutoFillAlreadyOrder("填充表单（已订购用户）", "/purchase/order/list", ApiTypeEnum.get, HandlerType.loading,
			PurchaseAutoFillAlreadyOrderResponseHandler.class),

	purchaseAutoFillPhone("填充表单（新用户）", "/account/user/info", ApiTypeEnum.get, HandlerType.loading, PurchaseAutoFillPhoneResponseHandler.class),

	purchaseOrderComment("提交订单评论", "/purchase/order/comment", ApiTypeEnum.post, HandlerType.waitting, CommonMsgResponseHandler.class),

	purchaseOrderPayPrice("获得订单支付金额", "/purchase/order/pay/price", ApiTypeEnum.get, HandlerType.loading, PurchaseOrderPayPriceResponseHandler.class),

	purchaseOrderPayAlipay("订单支付 - 支付宝", "/purchase/order/pay", ApiTypeEnum.get, HandlerType.waitting, PurchaseOrderPayAlipayResponseHandler.class),

	purchaseOrderOffline("订单支付 - 线下支付", "/purchase/order/pay", ApiTypeEnum.get, HandlerType.waitting, PurchaseOrderPayOfflineResponseHandler.class),

	purchaseOrderPayWeichat("订单支付 - 微信", "/purchase/order/pay", ApiTypeEnum.get, HandlerType.waitting, PurchaseOrderPayWeichatResponseHandler.class),

	purchaseOrderPayResult("订单支付结果", "/purchase/order/info", ApiTypeEnum.get, HandlerType.loading, PurchaseOrderPayResultHandler.class),

	account("我的", "/account/user/info", ApiTypeEnum.get, HandlerType.loading, AccountResponseHandler.class),

	accountLoginVcode("获得验证码", "/account/user/login/v_code", ApiTypeEnum.get, HandlerType.waitting, AccountLoginVCodeResponseHandler.class),

	accountLoginVcodeVoice("语音获得验证码", "/account/user/login/v_code/voice", ApiTypeEnum.get, HandlerType.waitting,
			AccountLoginVCodeVoiceResponseHandler.class),

	accountLogin("登录", "/account/user/login/info", ApiTypeEnum.get, HandlerType.waitting, AccountLoginResponseHandler.class),

	accountConsumeTicket("代金卷", "/account/user/consume_ticket/list", ApiTypeEnum.get, HandlerType.loading, AccountTicketResponseHandler.class),

	accountConsumeTicketExchange("代金卷兑换", "/account/user/consume_ticket/exchange", ApiTypeEnum.get, HandlerType.waitting,
			CommonMsgResponseHandler.class),

	accountFeedback("意见反馈", "/account/feedback", ApiTypeEnum.post, HandlerType.waitting, CommonMsgResponseHandler.class),

	accountNurseJoin("护士加入", "/account/user/join_info", ApiTypeEnum.post, HandlerType.waitting, CommonMsgResponseHandler.class),
	
	accountNurseJoinReady("护士加入前获得用户信息", "/account/user/info", ApiTypeEnum.get, HandlerType.waitting, AccountNurseJoinReadyResponseHandler.class),

	accountUserAgreement("用户协议", "/account/user/agreement", ApiTypeEnum.h5),

	accountUserConsumeTicketInstructions("代金劵-使用说明", "/account/user/consume_ticket/instructions", ApiTypeEnum.h5),

	accountUserJoinInfoAgreement("护士加入-许可协议", "/account/user/join_info/agreement", ApiTypeEnum.h5),

	accountShare("分享内容", "/account/user/share/info", ApiTypeEnum.get, HandlerType.waitting, AccountShareResponseHandler.class),

	accountUserGiftCash("现金返利", "/account/user/gift/cash", ApiTypeEnum.h5), ;

	/** 接口名 */
	private String name;
	/** 接口地址 */
	private String apiPath;
	/** 接口类型 */
	private ApiTypeEnum apiType;
	/** 接口处理器 */
	private Class<? extends MyJsonHttpResponseHandler<?, ?>> handlerClazz;
	/** 处理器类型 */
	private HandlerType handlerType;

	private ApiRequest(String name, String apiPath, ApiTypeEnum apiType, HandlerType handlerType,
			Class<? extends MyJsonHttpResponseHandler<?, ?>> handlerClazz) {
		this.apiPath = apiPath;
		this.apiType = apiType;
		this.handlerClazz = handlerClazz;
		this.name = name;
		this.handlerType = handlerType;
	}

	private ApiRequest(String name, String apiPath, ApiTypeEnum apiType, Class<? extends MyJsonHttpResponseHandler<?, ?>> handlerClazz) {
		this.apiPath = apiPath;
		this.apiType = apiType;
		this.handlerClazz = handlerClazz;
		this.name = name;
	}

	private ApiRequest(String name, String apiPath, ApiTypeEnum apiType) {
		this.apiPath = apiPath;
		this.apiType = apiType;
		this.name = name;
	}

	public <T> void request(T caller, RequestParams params, HandlerType handlerType) {
		request(caller, params, handlerType, true);
	}

	public <T> void request(T caller, RequestParams params, HandlerType handlerType, boolean showTip) {
		if (handlerClazz == null)
			return;

		String apiUrl = AppConfig.API_DOMAIN + AppConfig.API_PATH + apiPath;
		//		String apiUrl = AppConfig.API_DOMAIN;

		try {
			MyBaseHttpResponseHandler<T, ?> responseHandler = (MyBaseHttpResponseHandler<T, ?>) handlerClazz.newInstance();

			//Handler初始化
			responseHandler.init(caller);

			//设置结果处理类型
			responseHandler.setHandlerType(handlerType);

			//设置为测试模式（读本地接口数据）
			responseHandler.setTest(false);

			//是否显示toast
			responseHandler.setShowTip(showTip);

			LogUtils.info("接口请求|" + apiType.name() + "|" + apiUrl + "?" + params.toString());

			//接口类型
			if (ApiTypeEnum.get.equals(apiType))
				HttpHelper.getInstance().get(apiUrl, params, responseHandler);
			else if (ApiTypeEnum.post.equals(apiType))
				HttpHelper.getInstance().post(apiUrl, params, responseHandler);
			else
				LogUtils.error("错误", new Exception("非GET&POST接口禁止调用|" + name));

		} catch (Throwable e) {
			LogUtils.error(e.getMessage(), e);
			ViewUtils.toast(R.string.net_data_error);
		}
	}

	public <T> void request(T caller, RequestParams params) {

		request(caller, params, handlerType, true);

	}

	public <T> void request(T caller, RequestParams params, boolean showTip) {

		request(caller, params, handlerType, showTip);

	}

	/**
	 * H5页面 
	 * @param caller
	 * @param url
	 * @param title
	 */
	public void request(Activity caller, String title) {

		RequestParams params = new RequestParams();
		request(caller, params);

	}

	/**
	 * H5页面 
	 * @param caller
	 * @param url
	 * @param title
	 */
	public void request(Activity caller, RequestParams params, String title) {
		String apiUrl = AppConfig.API_DOMAIN + AppConfig.API_PATH + apiPath;

		if (params != null) {
			
			String p = params.toString();

			if (StringUtils.isNotBlank(p)) {
				apiUrl = apiUrl + "?" + p;
			}
		}

		LogUtils.info("接口请求|" + apiType.name() + "|" + apiUrl);

		if (ApiTypeEnum.h5.equals(apiType))
			MyPageHelper.webView.showMyWebViewPage(caller, apiUrl, title);
		else
			LogUtils.error("错误", new Exception("非H5接口禁止调用|" + name));

	}

	/**
	 * H5页面 
	 * @param caller
	 * @param url
	 */
	public void request(Activity caller) {

		request(caller, name);
	}

	/**
	 * H5页面 
	 * @param caller
	 * @param url
	 */
	public void request(Activity caller, RequestParams params) {

		request(caller, params, name);
	}

	/**
	 * 接口类型
	 * @author li.li
	 *
	 */
	private enum ApiTypeEnum {
		get, //获取数据
		post, //发送数据
		h5, //H5网址
		;
	}

}
